<?php

include_once('inc/functions.inc.php');
include_once("ros_pdf/class.ezpdf.php");

$arrSizePage[0] = 19.1;
$arrSizePage[1] = 13.9;
$objPDF = new Cezpdf();
$objPDF->selectFont("ros_pdf/fonts/Helvetica.afm");

$arrInfoCheque = array();
$cuenta = getParam("cuenta");
$numero = getParam("numero");

if (isEmpty($cuenta) || isEmpty($numero)) {
    trigger_error("Parametros de impresion incompletos!", E_USER_ERROR);
}

$strQuery = "SELECT * FROM cllg_bnc_cheque WHERE FK_CUENTA = '{$cuenta}' AND NUMERO = '{$numero}' AND ESTADO = '1'";
$qTMP = $db->query($strQuery);
$intNumRows = $db->num_rows;
if ($intNumRows > 0) {
    while ($rTMP = $db->fetch_array($qTMP)) {
        $arrInfoCheque = $rTMP;
    }
}
$db->free_result($qTMP);

$intMaxY = 786;
$intFontSize = 10;

$arrMeses = array();
$arrMeses[1] = "enero";
$arrMeses[2] = "febrero";
$arrMeses[3] = "marzo";
$arrMeses[4] = "abril";
$arrMeses[5] = "mayo";
$arrMeses[6] = "junio";
$arrMeses[7] = "julio";
$arrMeses[8] = "agosto";
$arrMeses[9] = "septiembre";
$arrMeses[10] = "octubre";
$arrMeses[11] = "noviembre";
$arrMeses[12] = "diciembre";

$arrFecha = explode("-", $arrInfoCheque["FECHA"]);
$strFecha = $arrFecha[2] . " de " . $arrMeses[intval($arrFecha[1])] . " de " . $arrFecha[0];

/**
 *                      X, Y 
 * La posicion          0, 0   es la esquina inferior izquierda
 * La hoja carta mide 612, 792
 * La hoja carta mide 608, 786
 */
//$objPDF->addText(608, 786, $intFontSize, "1dfsf");
//FECHA
$objPDF->addText(375, $intMaxY - 65, $intFontSize, $strFecha);
//MONTO EN NUMEROS
$objPDF->addText(470, $intMaxY - 105, $intFontSize, number_format($arrInfoCheque["MONTO"], 2));
//NOMBRE DEL CHEQUE
$objPDF->addText(130, $intMaxY - 105, $intFontSize, $arrInfoCheque["NOMBRE"]);

//MONTO EN LETRAS
$objPDF->addText(130, $intMaxY - 130, $intFontSize, strtoupper(PrecioEnLetras($arrInfoCheque["MONTO"])));
//CONCEPTO
$objPDF->addText(100, $intMaxY - 285, $intFontSize, $arrInfoCheque["CONCEPTO"]);

$strQuery = "SELECT cllg_contab_cuenta.codigo,
                    cllg_contab_cuenta.nombre, 
                    IF(cllg_contab_cuenta_ajuste.debe=1, cllg_contab_cuenta_ajuste.monto, 0) AS debe,  
                    IF(cllg_contab_cuenta_ajuste.haber=1, cllg_contab_cuenta_ajuste.monto, 0) AS haber  
             FROM   cllg_contab_ajuste,
                    cllg_contab_cuenta_ajuste,
                    cllg_contab_cuenta 
             WHERE  cllg_contab_ajuste.id = '{$arrInfoCheque["FK_AJUSTE"]}'
             AND    cllg_contab_ajuste.id = cllg_contab_cuenta_ajuste.ajuste
             AND    cllg_contab_cuenta_ajuste.cuenta = cllg_contab_cuenta.codigo";
$qTMP = $db->query($strQuery);
$intNumRows = $db->num_rows;
if ($intNumRows > 0) {
    $intSeparacion = 0;
    while ($rTMP = $db->fetch_array($qTMP)) {
        $intSeparacion += 20;
        $rTMP["debe"] = ( $rTMP["debe"] > 0 ) ? number_format($rTMP["debe"], 2) : "";
        $rTMP["haber"] = ( $rTMP["haber"] > 0 ) ? number_format($rTMP["haber"], 2) : "";
        $objPDF->addText(075, $intMaxY - 335 - $intSeparacion, $intFontSize, $rTMP["codigo"]);
        $objPDF->addText(120, $intMaxY - 335 - $intSeparacion, $intFontSize, $rTMP["nombre"]);
        $objPDF->addText(350, $intMaxY - 335 - $intSeparacion, $intFontSize, $rTMP["debe"]);
        $objPDF->addText(380, $intMaxY - 335 - $intSeparacion, $intFontSize, $rTMP["haber"]);
    }
}
$db->free_result($qTMP);


$objPDF->ezStream(array("Content-Disposition"=>"Content-Disposition: attachment; filename=cheque-$numero-$cuenta.pdf"));

/**
 * @return string
 * @param integer $intNumber El n�mero a convertir, puede tener 2 decimales tambi�n
 * @desc Devuelve un numero en letras (estandar para dinero)
 */
function PrecioEnLetras($intNumber, $strMonedaName = "") {
    $arrParts = explode(".", $intNumber);
    $arrParts["int"] = $arrParts[0];
    if (!isset($arrParts[1])) {
        $arrParts["dec"] = "00";
    } else {
        $intDecLen = strlen($arrParts[1]);
        if ($intDecLen == 1) {
            $arrParts["dec"] = $arrParts[1] . "0";
        } else {
            $arrParts["dec"] = $arrParts[1];
        }
    }
    unset($arrParts[0]);
    unset($arrParts[1]);

    $strReturn = NumeroEnLetras($intNumber);
    if ($strMonedaName)
        $strReturn.= " " . $strMonedaName . " con {$arrParts["dec"]}/100";
    else
        $strReturn.=" con {$arrParts["dec"]}/100";

    return trim($strReturn);
}

/**
 * @return string
 * @param integer $intNumber El n�mero a convertir, puede tener 2 decimales tambi�n
 * @param array $arrStrings (NO USAR ESTE PARAMETRO, ES PARA USO DE LA RECURSION EN LA FUNCION)
 * @desc Devuelve un numero en letras (estandar para dinero)
 */
function NumeroEnLetras($intNumber, $arrStrings = false) {
    $boolFirst = false;
    if (!$arrStrings) {
        $boolFirst = true;
        $intNumber = round($intNumber, 2) . "";
        $arrStrings = array();
        $arrStrings[0][1] = "uno";
        $arrStrings[0][2] = "dos ";
        $arrStrings[0][3] = "tres ";
        $arrStrings[0][4] = "cuatro ";
        $arrStrings[0][5] = "cinco ";
        $arrStrings[0][6] = "seis ";
        $arrStrings[0][7] = "siete ";
        $arrStrings[0][8] = "ocho ";
        $arrStrings[0][9] = "nueve ";

        $arrStrings[1][1] = "dieci";
        $arrStrings[1][2] = "veinti";
        $arrStrings[1][3] = "treinta y ";
        $arrStrings[1][4] = "cuarenta y ";
        $arrStrings[1][5] = "cincuenta y ";
        $arrStrings[1][6] = "sesenta y ";
        $arrStrings[1][7] = "setenta y ";
        $arrStrings[1][8] = "ochenta y ";
        $arrStrings[1][9] = "noventa y ";

        $arrStrings[2][1] = "ciento ";
        $arrStrings[2][2] = "doscientos ";
        $arrStrings[2][3] = "trescientos ";
        $arrStrings[2][4] = "cuatrocientos ";
        $arrStrings[2][5] = "quinientos ";
        $arrStrings[2][6] = "seiscientos ";
        $arrStrings[2][7] = "setecientos ";
        $arrStrings[2][8] = "ochocientos ";
        $arrStrings[2][9] = "novecientos ";

        $arrStrings[3][1] = "mil ";
    }

    if ($intNumber == 0)
        return "cero";

    $arrParts = explode(".", $intNumber);
    $arrParts["int"] = $arrParts[0];
    if (!isset($arrParts[1])) {
        $arrParts["dec"] = "00";
    } else {
        $intDecLen = strlen($arrParts[1]);
        if ($intDecLen == 1) {
            $arrParts["dec"] = $arrParts[1] . "0";
        } else {
            $arrParts["dec"] = $arrParts[1];
        }
    }
    unset($arrParts[0]);
    unset($arrParts[1]);

    $strTMP = $arrParts["int"];
    $arrParts["int"] = array();
    for ($i = strlen($strTMP); $i > 0; $i--) {
        $arrParts["int"][$i - 1] = substr($strTMP, strlen($strTMP) - $i, 1);
    }
    ksort($arrParts["int"]);

    $strReturn = "";
    while (($arrThis = each($arrParts["int"])) && $arrThis["key"] < 3) {
        $strTMP = "";
        if ($arrThis["key"] == 1 && $arrThis["value"] == 1 && $arrParts["int"][0] < 6) {
            switch ($arrParts["int"][0]) {
                case 0:
                    $strReturn = "diez";
                    break;
                case 1:
                    $strReturn = "once";
                    break;
                case 2:
                    $strReturn = "doce";
                    break;
                case 3:
                    $strReturn = "trece";
                    break;
                case 4:
                    $strReturn = "catorce";
                    break;
                case 5:
                    $strReturn = "quince";
                    break;
            }
        } elseif ($arrThis["key"] == 1 && $arrThis["value"] == 2 && $arrParts["int"][0] == 0) {
            $strReturn = "veinte";
        } elseif ($arrThis["key"] == 2 && $arrThis["value"] == 1 && $arrParts["int"][1] == 0 && $arrParts["int"][0] == 0) {
            $strReturn = "cien";
        } else {
            $strTMP = (isset($arrStrings[$arrThis["key"]][$arrThis["value"]])) ? $arrStrings[$arrThis["key"]][$arrThis["value"]] : "";
        }

        if (empty($strReturn)) {
            $strTMP = str_replace(" y ", "", $strTMP);
        }
        $strReturn = $strTMP . $strReturn;
    }

    $strMiles = "";
    $strMillones = "";
    for ($i = 3; $i < count($arrParts["int"]); $i++) {
        if ($i < 6) {
            $strMiles = $arrParts["int"][$i] . $strMiles;
        } else {
            $strMillones = $arrParts["int"][$i] . $strMillones;
        }
    }

    if (!empty($strMiles)) {
        if ($strMiles == 1) {
            $strReturn = "un mil " . $strReturn;
        } else if ($strMiles > 0) {
            $strReturn = NumeroEnLetras($strMiles, $arrStrings) . " mil " . $strReturn;
        }
    }

    if (!empty($strMillones)) {
        if ($strMillones == 1) {
            $strReturn = "un millón " . $strReturn;
        } else if ($strMillones > 0) {
            $strReturn = NumeroEnLetras($strMillones, $arrStrings) . " millones " . $strReturn;
        }
    }

    return trim($strReturn);
}

?>